<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>

<script>

    $(function() {
        heBing("itemTable1");
    });

    /*
     * 加载病种结果下拉框
     */
    $("input[name='aa']").select2({
        minimumInputLength: 1,
        multiple             : true,
        separator            : ";",
        initSelection        : function (element, callback) {   // 初始化时设置默认值
//            $(element.val().split(";")).each(function () {
//                }
                $.ajax({
                    url:"${ctx}/oper/diseaseManagement/automatic",
                    data:{ids:element.val()},
                    type: 'post', // 提交方式 get/post
                    success:function(data){
                        var row = jQuery.parseJSON(data);
                        callback(row.items)
                    }
                })

//            });

            },
        createSearchChoice   : function(term, data) {           // 创建搜索结果（使用户可以输入匹配值以外的其它值）
            return { id: term, text: term };
        },
        ajax : {
            url      : "${ctx}/oper/diseaseManagement/automatic",              // 异步请求地址
            data: function (term) {
                return {
                    q: term
                };
            },
            results: function (data) {
                return {
                    results: data.items
                };

            }
        },
        formatSelection : resultFormatSelection,  // 选择结果中的显示
        formatResult    : resultFormatResult,
        escapeMarkup : function (m) { return m; }

    });
    function resultFormatResult(medata) {
        return medata.text;
    }
    var jy= "";
    var xj = "";
    function resultFormatSelection(medata) {
        xj="<div name='"+medata.id+"'>"+medata.text+"<div></br>"
        if(typeof(medata.jy)!="undefined"){
            jy= "<div name='"+medata.id+"'>"+medata.jy+"<div></br>";
        } else {
            jy="";
        }
        $("#jiankangjianyi").append(jy);
        $("#xiaojie").append(xj);
        return medata.text+"<input type='hidden' value='"+medata.id+"'/>";

//
    }



    // 移除完毕事件
    $("input[name='aa']").on("select2-removed", function(e) {
        $("#xiaojie").find("div[name='"+e.val+"']").remove();
        xj = $("#xiaojie").find("div").html();
        $("#jiankangjianyi").find("div[name='"+e.val+"']").remove();
        jy = $("#jiankangjianyi").find("div").html();
    })

    /**
     *合并相同列
     */
    $(function() {
        heBing("itemTable1");
    });



    /**
    *列表保存
    * @param patientId
    * @param clinicId
    * @returns {boolean}
     */
    function addTreat(patientId,clinicId,deptId){
        var rows = $("#itemTable1").find("tbody tr");
        if(rows.length >0){
            var text = "";
            var xiaojie = $("#xiaojie").html();
            var jianyi = $("#jianyi").html();
            var jiankangjianyi = $("#jiankangjianyi").html();
            var wenxintishi = $("#wenxintishi").html();
            for(var i=0;i<rows.length;i++){
                var row = rows.eq(i).find("td");
                var itemName = row.eq(1).text();//项目名称
                var itemResults = row.eq(2).find("div ul li div");//项目结果
//                var itemResults = row.eq(2).find("div ul li div");
                var itemResult="";
                if(itemResults.length>0){
                    for(var j=0;j<itemResults.length;j++){
                        itemResult += itemResults.eq(j).find("input").val()+";";//项目结果
                    }
                }
                var itemUnit = row.eq(3).find("input").val();//单位
                var fuzhen = row.eq(4).find("input").val()//是否复检
                var itemRange = row.eq(6).text();//范围
                var unusual = row.eq(7).text();//异常
                var groupId = row.eq(8).text();//主表id
                var itemCode = row.eq(9).text();
//                var clinicId = row.eq(10).text();
                var officeId = row.eq(11).text();
                var itemId = row.eq(12).text();
                text+="<tr><td>" +
                "<input type='hidden' name='tResultItemList["+i+"].id'"+" value='"+itemId+"'/>" +
                "<input type='hidden' name='tResultItemList["+i+"].itemName'"+" value='"+itemName+"'/>" +
                "<input type='hidden' name='tResultItemList["+i+"].itemResult'"+" value='"+itemResult+"'/>" +
//                "<input type='hidden' name='tResultItemList["+i+"].itemResultId'"+" value='"+itemResultId+"'/>" +
                "<input type='hidden' name='tResultItemList["+i+"].itemUnit'"+" value='"+itemUnit+"'/>" +
                "<input type='hidden' name='tResultItemList["+i+"].fuzhen'"+" value='"+fuzhen+"'/>" +
                "<input type='hidden' name='tResultItemList["+i+"].itemRange'"+" value='"+itemRange+"'/>" +
                "<input type='hidden' name='tResultItemList["+i+"].unusual'"+" value='"+unusual+"'/>" +
                "<input type='hidden' name='tResultItemList["+i+"].groupId'"+" value='"+groupId+"'/>" +
                "<input type='hidden' name='tResultItemList["+i+"].itemCode'"+" value='"+itemCode+"'/>" +
                "<input type='hidden' name='tResultItemList["+i+"].clinicId'"+" value='"+clinicId+"'/>" +
                "<input type='hidden' name='tResultItemList["+i+"].tResultOffice.xiaojie'"+" value='"+xiaojie+"'/>" +
                "<input type='hidden' name='tResultItemList["+i+"].tResultOffice.jiankangjianyi'"+" value='"+jiankangjianyi+"'/>" +
                "<input type='hidden' name='tResultItemList["+i+"].tResultOffice.wenxintishi'"+" value='"+wenxintishi+"'/>" +
                "<input type='hidden' name='tResultItemList["+i+"].tResultOffice.id'"+" value='"+officeId+"'/>" +
                "</td></tr>";
            }
            var list = "<table>"+text+"</table>";
            $("#itemTableForm").html(list);
            var divUrl= "${ctx}/physical/sorting?patientId="+patientId+"&clinicId="+clinicId+"&deptId="+deptId;
            $("#itemTableForm").ajaxSubmit({
                type: 'post', // 提交方式 get/post
                url:'${ctx}/tresultitem/tresultitem/tResultItem/save', // 需要提交的 url
                success: function(data) { // data 保存提交后返回的数据，一般为 json 数据
                    if (data.code == "success") {
                        toastr.success(data.data);
                        loadDiv("rigthDoctorCenterDiv", divUrl);
                    } else if(data.code == 'info'){
                        loadDiv("rigthDoctorCenterDiv", divUrl);
                    } else {
                        flag = true;
                        toastr.error(data.data);
                    }
                }
            });
        } else{
            toastr.error("请选择提交项目后再保存");
            return false;
        }

    }


</script>
<form id="itemTableForm">

</form>
<div class="panel-body">
    <div class="datatable-top">
        <div class="pull-left">
            <form id="searchForm"
                       onsubmit="return loadDivForm('rigthDoctorCenterDiv','searchForm','${ctx}/physical/sorting');"
                       method="post" class="form-inline">
                <%--<input id="pageNo" name="pageNo" type="hidden" value="${page.pageNo}"/>--%>
                <%--<input id="pageSize" name="pageSize" type="hidden" value="${page.pageSize}"/>--%>
                <input id="clinicId" name="clinicId" type="hidden" value="${clinicId}"/>
                <input id="patientId" name="patientId" type="hidden" value="${patientId}"/>
                &nbsp;&nbsp;<label>科室 ：</label><sys:treeselect selectedParent="true" id="deptId" name="deptId"
                                                               value="${deptId}"
                                                               update="false"
                                                               labelName="${deptId}"
                                                               labelValue="${fns:getDeptName(deptId,'')}"
                                                               title="科室" url="/sys/orgDept/treeData" isnull="true"
                                                               extId="${orgDept.id}" cssClass="form-control" />
                <input id="btnSubmit" class="btn btn-primary" type="submit" value="查询"/>
                <input class="btn btn-new" onclick="addTreat('${patientId}','${clinicId}','${deptId}')" type="button" value="保存"/>

            </form>
        </div>
    </div>
    <table id="itemTable1" class="table table-bordered mg-t datatable " style="width: 100%">
        <thead>
        <tr>
            <th>收费项目</th>
            <th>收费名称</th>
            <th>项目结果</th>
            <th>项目单位</th>
            <th>复诊</th>
            <th>结果类型</th>
            <th>范围</th>
            <th>异常</th>
            <th style="display: none">id</th>
            <th style="display: none">itemCode</th>
            <th style="display: none">clinicId</th>
            <th style="display: none">结果id</th>
            <th style="display: none">officeId</th>
            <th style="display: none">itemId</th>
        </tr>
        </thead>
        <tbody>
            <c:forEach items="${lists}" var="list" varStatus="index">
                <tr>
                    <td>${list.itemName}</td>
                    <td>${list.clinicName}</td>
                    <td><input style="width: 200px;" type="text" value="${list.itemResult}" name="aa"></td>
                    <td><input type="hidden" value="${list.itemUnit}">${fns:getDictLabels(list.itemUnit, 'SPEC_UNIT','' )}  </td>
                    <td align="center"><input type="checkbox"  name="itemList[${index.count}].fuzhen" value="${item.fuzhen}" <c:if test="${item.fuzhen==1 || item.fuzhen eq '1'}">checked</c:if>></td>
                    <td></td>
                    <td> </td>
                    <td> </td>
                    <td style="display: none">${list.groupId}</td>
                    <td style="display: none">${list.itemCode}</td>
                    <td style="display: none">${list.clinicId}</td>
                    <td style="display: none">${list.officeId}</td>
                    <td style="display: none">${list.tItemId}</td>
                </tr>
            </c:forEach>
        </tbody>
    </table>
    <div class="pagination">${page}</div>
</div>